<?php

class foundation_UserController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    	defined('BASE_URL')	|| define('BASE_URL', Zend_Controller_Front::getInstance()->getBaseUrl());
    }

    public function indexAction()
    {
		// action body
		$this->_helper->layout()->disableLayout();
		$db = new foundation_Model_DbTable_DbUser();
		if($this->getRequest()->isPost()){
			$id = $this->getRequest()->getPost();
			//print_r($id);exit();
			if($this->getRequest()->getPost("status")){
				$id = $this->getRequest()->getParam("status");
				//print_r($id);exit();
				if(!empty($id)){
					 $db->updateUnStatus($id);
					//echo "<script type='text/javascript'> alert('Update Status Faculty Successfull'); </script>";
				}
				//$this->_redirect("faculty/fac/index");
			}
			if($this->getRequest()->getPost("unstatus")){
				$id = $this->getRequest()->getParam("unstatus");
				//print_r($id);exit();
				$db->updateStatus($id);
				//echo "<script type='text/javascript'> alert('Update Status Faculty Successfull'); </script>";
			}
		}        
        $rows = $db->getuserss();
        ///print_r($rows);exit();
        $this->view->formfilter = $rows;
        
    }
    
    public function viewUserAction()
    {   
    	/* Initialize action controller here */
    	if($this->getRequest()->getParam('id')){
    		$db = new foundation_Model_DbTable_DbUser();
    		$user_id = $this->getRequest()->getParam('id');
    		$rs=$db->getUser($user_id);
    		$this->view->rs=$rs;
    	}  	 
    	
    }
	public function addUserAction()
	{	
		//$this->_helper->layout()->disableLayout();
		$db = new foundation_Model_DbTable_DbUser();
		if($this->getRequest()->isPost())
		{
			$post=$this->getRequest()->getPost();
			//print_r($post);exit();
			if(!$db->ifUserExist($post['username'],$post['email']))
			{
				$id=$db->insertUser($post);
				$tr = Application_Form_FrmLanguages::getCurrentlanguage();
				//Application_Form_FrmMessage::redirector('/foundation/user/index');
			}
			else {
				Application_Form_FrmMessage::message('User had existed already');
			}
		}
		$form= new foundation_Form_FrmUser();
		$this->view->form=$form;
	}
	// Edit user
    public function editUserAction()
    {
    	//$this->_helper->layout()->disableLayout();
    	$user_id=$this->getRequest()->getParam('id');
    	if(!$user_id)$user_id=0;
   		$form = new foundation_Form_FrmUser();
    	$db = new foundation_Model_DbTable_DbUser();
		$rs = $db->getUserInfo('SELECT * FROM rsv_acl_user where user_id='.$user_id);
		Application_Model_Decorator::setForm($form, $rs);
		
    	$this->view->form = $form;
    	$this->view->user_id = $user_id;
    	if($this->getRequest()->isPost())
		{
			$post = $this->getRequest()->getPost();
			//print_r($post);exit();
			$db->updateUser($post,$user_id);
			$tr = Application_Form_FrmLanguages::getCurrentlanguage();
			Application_Form_FrmMessage::message($tr->translate('Updat Seccessful'));
			//Application_Form_FrmMessage::redirector('/foundation/user/index');
			//$this->_redirect('/foundation/user/index');
			if($this->getRequest()->getParam("cancel")){
				$this->_redirect('/foundation/user/index');
			}
		}
		Application_Model_Decorator::removeAllDecorator($form);
		
    }

 
    public function changePasswordAction()
	{
		$session_user=new Zend_Session_Namespace('auth');
		
		if($session_user->user_id==$this->getRequest()->getParam('id') OR $session_user->level == 1){
			$form = new foundation_Form_FrmChgpwd();	
			$this->view->form=$form;
			
			if($this->getRequest()->isPost())
			{
				$db=new foundation_Model_DbTable_DbUser();
				$user_id=$this->getRequest()->getParam('id');		
				if(!$user_id) $user_id=0;			
				$current_password=$this->getRequest()->getParam('current_password');
				$password=$this->getRequest()->getParam('password');
				if($db->isValidCurrentPassword($user_id,$current_password)){ 
					$db->changePassword($user_id, md5($password));	
					Application_Form_FrmMessage::message('Password has been changed');
					Application_Form_FrmMessage::redirector('/foundation/user/view-user/id/'.$user_id);
				}else{
					Application_Form_FrmMessage::message('Invalid current password');
				}
			}		
		}else{ 
			Application_Form_FrmMessage::message('Access Denied!');
		    Application_Form_FrmMessage::redirector('/foundation');	
		}
		
	}

}

